package com.mengxuegu.blog.oauth2.config;

import com.alibaba.fastjson.JSON;
import com.mengxuegu.blog.entities.SysUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;

import java.util.Map;

public class AuthUtil {
    public static SysUser getUserInfo(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) authentication.getDetails();
        Map<String,Object> map = (Map<String,Object>)details.getDecodedDetails();
        Map<String, Object> userInfo = (Map<String, Object>) map.get("userInfo");

        SysUser sysUser = JSON.parseObject(JSON.toJSONString(userInfo), SysUser.class);
        return sysUser;

    }
}
